<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property integer $id
 * @property string $name
 * @property string $image
 * @property string $price
 * @property string $sales_price
 * @property string $description
 * @property integer $is_hot
 * @property string $create_date
 * @property integer $status
 * @property integer $user_id
 * @property integer $category_id
 */
class Product extends CActiveRecord
{
    const IS_HOT = 1;
    const NOT_HOT = 0;
    const STATUS_ACTIVED = 1;
    const STATUS_INACTIVED = 0;
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'product';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, price, is_hot, create_date, status, user_id, category_id', 'required',
                'message'=>'{attribute} không được để trống'),
			array('is_hot, status, user_id, category_id, price, sales_price', 'numerical', 'integerOnly'=>true,
                'message'=>'{attribute} phải là kí tự số 0-9'),
			array('name', 'length', 'max'=>50, 'tooLong'=>'{attribute} không quá 50 kí tự'),
			array('price, sales_price', 'length', 'max'=>10, 'tooLong'=>'{attribute} không quá 10 kí tự số'),
            array('image', 'file', 'allowEmpty'=>false, 'on'=>'create',
                'message'=>'{attribute} không được để trống'),
            array('image', 'file', 'allowEmpty'=>true, 'on'=>'update'),
            array('image', 'file', 'types'=>'jpg, jpeg, png, gif, JPG, JPEG, PNG, GIF',
                'maxSize'=>410000,
                'message'=>'{attribute} có dạng jpg, jpeg, gif, png',
                'tooLarge'=>'{attribute} không quá 400KB'),
            // The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, is_hot, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'product_category'=>array(self::BELONGS_TO,'ProductCategory','category_id'),
            'user'=>array(self::BELONGS_TO,'User','user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên sản phẩm',
			'image' => 'Hình ảnh',
			'price' => 'Giá tiền',
			'sales_price' => 'Giá ưu đãi',
			'description' => 'Mô tả sản phẩm',
			'is_hot' => 'Sản phẩm hot',
			'create_date' => 'Ngày cập nhật',
			'status' => 'Trạng thái',
			'user_id' => 'Người tạo',
			'category_id' => 'Chuyên mục',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('sales_price',$this->sales_price,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('is_hot',$this->is_hot);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('category_id',$this->category_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'id DESC',
            ),
            'pagination' => array(
                'pageSize' => 40,
            ),
		));
	}
    
    public function getIsHotArray($label = '') {
        if($label) $isHotArray[''] = $label;
        $isHotArray[self::NOT_HOT] = 'Sản phẩm thường';
        $isHotArray[self::IS_HOT] = 'Sản phẩm hot';
        return $isHotArray;
    }
    
    public function getStatusArray($label = '') {
        if($label) $statusArray[''] = $label;
        $statusArray[self::STATUS_ACTIVED] = 'Đã duyệt';
        $statusArray[self::STATUS_INACTIVED] = 'Chưa duyệt';
        return $statusArray;
    }
}